package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.*;
import com.api.shopapi.service.NursingRecordPositionLackService;
import com.api.shopapi.service.NursingRecordPositionService;
import com.api.shopapi.service.NursingRecordService;
import com.api.shopapi.service.StaffService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 护理记录的定位的缺卡记录
 */

@RequestMapping("NursingRecordPositionLack")
@RestController
public class NursingRecordPositionLackController extends BaseController{

    @Autowired
    @Qualifier("NursingRecordPositionLackServiceImplOne")
    private NursingRecordPositionLackService nursingRecordPositionLackService;


    @Autowired
    @Qualifier("NursingRecordPositionServiceImplOne")
    private NursingRecordPositionService nursingRecordPositionService;

    @Autowired
    @Qualifier("NursingRecordServiceImplOne")
    private NursingRecordService nursingRecordService;


    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;


    //gov今天的缺卡报警
    @RequestMapping(value = "ListToday", method = RequestMethod.POST)
    public MyResult ListToday(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String governmentCode = jsonObject.getString("governmentCode");

            PreCheck preCheck =checkSignFrontInterface(timeStr,sign,"/NursingRecordPositionLack/ListToday");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String startTime = sdf.format(new Date());

            Calendar calendar = new GregorianCalendar();
            calendar.setTime(sdf.parse(startTime));
            calendar.add(calendar.DATE, 1);
            String endTime = sdf.format(calendar.getTime());

            List<NursingRecordPositionLack> list =
                    nursingRecordPositionLackService.listByGovCodeTime(governmentCode,startTime,endTime);

            List<NursingRecordPositionLackDTO> listDTO = new ArrayList<>();
            for(NursingRecordPositionLack nursingRecordPositionLack:list){

                NursingRecordPositionLackDTO nursingRecordPositionLackDTO =
                        mapper.map(nursingRecordPositionLack,NursingRecordPositionLackDTO.class);

                NursingRecordPosition nursingRecordPosition =
                        nursingRecordPositionService.getByNursingRecordPositionCode(nursingRecordPositionLackDTO.getNursingRecordPositionCode());
                NursingRecordPositionDTO nursingRecordPositionDTO = mapper.map(nursingRecordPosition,NursingRecordPositionDTO.class);
                NursingRecord nursingRecord = nursingRecordService.getByNursingRecordCode(nursingRecordPositionDTO.getNursingRecordCode());
                nursingRecordPositionDTO.setNursingRecord(nursingRecord);

                nursingRecordPositionLackDTO.setNursingRecordPositionDTO(nursingRecordPositionDTO);

                listDTO.add(nursingRecordPositionLackDTO);

            }

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("list",listDTO);

            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/NursingRecordPositionLack/ListToday", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //翻页
    @RequestMapping(value = "GovPage", method = RequestMethod.POST)
    public MyResult GovPage(@RequestBody JSONObject jsonObject) {

        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/NursingRecordPositionLack/GovPage");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            PageInfo<NursingRecordPositionLack> pageInfo = nursingRecordPositionLackService.pageByGovCode(pageIndex,pageSize, staff.getGovernmentCode());

            List<NursingRecordPositionLackDTO> listDTO = new ArrayList<>();
            for(NursingRecordPositionLack nursingRecordPositionLack:pageInfo.getList()){

                NursingRecordPositionLackDTO nursingRecordPositionLackDTO =
                        mapper.map(nursingRecordPositionLack,NursingRecordPositionLackDTO.class);

                NursingRecordPosition nursingRecordPosition =
                        nursingRecordPositionService.getByNursingRecordPositionCode(nursingRecordPositionLackDTO.getNursingRecordPositionCode());
                NursingRecordPositionDTO nursingRecordPositionDTO = mapper.map(nursingRecordPosition,NursingRecordPositionDTO.class);
                NursingRecord nursingRecord = nursingRecordService.getByNursingRecordCode(nursingRecordPositionDTO.getNursingRecordCode());
                nursingRecordPositionDTO.setNursingRecord(nursingRecord);

                nursingRecordPositionLackDTO.setNursingRecordPositionDTO(nursingRecordPositionDTO);

                listDTO.add(nursingRecordPositionLackDTO);

            }

            pageInfo.setList(Collections.unmodifiableList(listDTO));

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo", pageInfo);
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/NursingRecordPositionLack/GovPage", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }


}
